package com.snail.billing.session;

import org.json.JSONException;
import org.json.JSONObject;

import com.snail.billing.DataCache;
import com.snail.billing.data.UUIDManager;
import com.snail.billing.json.JsonBase;
import com.snail.billing.net.BillingArrayHttpSession;
import com.snail.billing.net.BillingEncode;
import com.snail.billing.net.BillingSecurity;

public class ValidateInfoSession extends BillingArrayHttpSession {

	public ValidateInfoSession() {
		DataCache cache = DataCache.getInstance();
		String host = cache.importParams.hostBusiness;
		String address = String.format("http://%s/passport/login/getValidateInfo.do", host);
		setAddress(address);
		addBillingPair("sessionId", BillingEncode.enCode(cache.importParams.sessionId, "GBK"));
		addBillingPair("uuid", BillingEncode.enCode(UUIDManager.getUUID(), "GBK"));
		addBillingPair("aid", cache.importParams.aid);
		addBillingPair("gameId", cache.importParams.gameId);
		buildParamPair();
	}

	@Override
	public BillingSecurity getSecurity() {
		if (DataCache.getInstance().isSandbox) {
			return BillingSecurity.securitySandbox2;
		}
		return BillingSecurity.security;
	}

	public static class JsonData extends JsonBase {

		private String sessionId;
		private String account;
		private boolean isMobileAuthed;
		private String mobile;
		private boolean isEmailAuthed;
		private String email;

		public JsonData(String jsonString) {
			super(jsonString);
			try {
				if (getCode() == 1) {
					JSONObject json = new JSONObject(jsonString);
					if (json.has("sessionId")) {
						sessionId = json.getString("sessionId");
					}
					if (json.has("account")) {
						account = json.getString("account");
					}
					if (json.has("info")) {
						JSONObject json2 = new JSONObject(json.getString("info"));
						if (json2.has("isMobileAuthed")) {
							isMobileAuthed = json2.getString("isMobileAuthed").equals("1");
						}
						if (json2.has("mobile")) {
							mobile = json2.getString("mobile");
						}
						if (json2.has("isEmailAuthed")) {
							isEmailAuthed = json2.getString("isEmailAuthed").equals("1");
						}
						if (json2.has("email")) {
							email = json2.getString("email");
						}
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		public String getSessionId() {
			return sessionId;
		}

		public String getAccount() {
			return account;
		}

		public boolean isMobileBound() {
			return isMobileAuthed;
		}

		public String getMobile() {
			return mobile;
		}

		public boolean isEmailBound() {
			return isEmailAuthed;
		}

		public String getEmail() {
			return email;
		}

	}

}
